﻿// This software is the confidential and proprietary information of NetBay.
// Such confidential information should not be disclosed.
// NetBay copyright(c) 2012

#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Resources;

#endregion

namespace Rio.Framework.Localisation
{
    public class Resx
    {
        #region Attributes

        public static ResourceManager RioResourceManager { get; set; }

        #endregion

        #region Public functions

        public static string GetLabel(Enum localizationEnum)
        {
            return GetLabel(GetEnumDescription(localizationEnum));
        }

        public static string GetLabel(string localizationString)
        {
            if (String.IsNullOrWhiteSpace(localizationString))
            {
                throw new Exception(String.Format("Resource Key is empty : {0}.", localizationString));
            }

            try
            {
                return RioResourceManager.GetString(localizationString);
            }
            catch (Exception)
            {
                return "<not set>";
            }
        }

        #endregion

        public static string GetEnumDescription(Enum eInstance)
        {
            FieldInfo fi = eInstance.GetType().GetField(eInstance.ToString());
            DescriptionAttribute da =
                (DescriptionAttribute) Attribute.GetCustomAttribute(fi, typeof (DescriptionAttribute));

            string description = da != null ? da.Description : eInstance.ToString();

            return description;
        }
    }
}